import request from "../../utils/request";
Page({
  data: {
    currentTab: 'lost',
    items: [
      {
        id: 1,
        image: '/images/losing1.png',
        name: '雨伞',
        category: 'lost',
        time: '2024-10-01',
        location: '图书馆门口',
        phone: '13800138000',
        desc: '普通黑色雨伞'
      },
      {
        id: 2,
        image: '/images/losing2.png',
        name: '校园一卡通',
        category: 'lost',
        time: '2024-10-02',
        location: '食堂',
        phone: '13900139000',
        desc: '招商银行一卡通'
      },
      {
        id: 3,
        image: '/images/losing3.png',
        name: '钥匙',
        category: 'found',
        time: '2024-10-03',
        location: '操场',
        phone: '13700137000',
        desc: '一串钥匙'
      },
      {
        id: 4,
        image: '/images/losing4.png',
        name: '钱包',
        category: 'found',
        time: '2024-10-04',
        location: '教学楼',
        phone: '13600136000',
        desc: '棕色钱包'
      },
      {
        id: 5,
        image: '/images/losing5.png',
        name: '眼镜',
        category: 'lost',
        time: '2024-10-05',
        location: '自习室',
        phone: '13500135000',
        desc: '近视眼镜'
      },
      {
        id: 6,
        image: '/images/losing6.png',
        name: '帽子',
        category: 'found',
        time: '2024-10-06',
        location: '体育馆',
        phone: '13400134000',
        desc: '蓝色棒球帽'
      }
    ],
    filteredItems: [],
    searchKey: '',
    isSearching: false
  },
  async onLoad() {
    //lostfounds
    let data=await request.get("lostfounds/")
    data = data.map(item => ({
      ...item,
      phone:item.tel,
      desc:item.description,
      time: item?.time.substring(0,10) // 新增字段
    }));
    this.setData({
      items:data
    })
    this.filterItems();
  },
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      currentTab: tab,
      searchKey: ''
    });
    this.filterItems();
  },
  filterItems() {
    const { currentTab, items, searchKey } = this.data;
    let filtered = items.filter(item => item.category === currentTab);
    if (searchKey) {
      filtered = filtered.filter(item => 
        item.name.includes(searchKey) || 
        item.location.includes(searchKey)
      );
    }
    this.setData({
      filteredItems: filtered
    });
  },
  handleInputChange(e) {
    this.setData({
      searchKey: e.detail.value
    });
  },
  handleSearchSubmit() {
    this.setData({
      isSearching: true
    });
    setTimeout(() => {
      this.setData({
        isSearching: false
      });
    }, 200);
    this.filterItems();
  },
  navigateToDetail(e) {
    const item = e.currentTarget.dataset.item;
    const itemStr = encodeURIComponent(JSON.stringify(item));
    wx.navigateTo({
      url: `/pages/lostDetail/lostDetail?item=${itemStr}`
    });
  }
})    